//
//  LVVerifiyCodeViewController.m
//  LEVE
//
//  Created by 王若聪 on 2017/2/18.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVVerifiyCodeViewController.h"
#import "SVProgressHUD.h"
#import "UIView+Toast.h"
#import "LVResetPasswordViewController.h"
#import "UIButton+Addition.h"
@interface LVVerifiyCodeViewController ()
@property (nonatomic, strong)UILabel      *titleLabel;
@property (nonatomic, strong)UILabel      *phoneStatesCodeNumLabel;
@property (nonatomic, strong)UILabel      *phoneNumLabel;
@property (nonatomic, strong)UITextField  *verifyCodeTextFiled;
@property (nonatomic, strong)UIButton     *resentVerifyCodeBtn;
@property (nonatomic, strong)UILabel      *timeLimitLabel;
@property (nonatomic, strong)UIButton     *submitBtn;
@end

@implementation LVVerifiyCodeViewController
- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.text = @"验证码短信已发送至";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor  = UIColorFromRGB(0x4A4A4A);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@112);
        make.height.equalTo(@20);
        make.width.equalTo(self.view.mas_width);
    }];
    
    self.phoneStatesCodeNumLabel = [[UILabel alloc]init];
    self.phoneStatesCodeNumLabel.text = @"+86";
    self.phoneStatesCodeNumLabel.font = [UIFont systemFontOfSize:14];
    self.phoneStatesCodeNumLabel.textColor  = UIColorFromRGB(0x7665A3);
    [self.view addSubview:self.phoneStatesCodeNumLabel];
    [self.phoneStatesCodeNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.height.equalTo(@20);
        NSNumber *leftValue = kScreenWidth > 320 ? @127 : @83;
        make.left.equalTo(leftValue);
        make.width.equalTo(@45);
    }];
    
    self.phoneNumLabel = [[UILabel alloc]init];
    self.phoneNumLabel.text = @"17620371033";
    self.phoneNumLabel.font = [UIFont systemFontOfSize:14];
    self.phoneNumLabel.textColor  = UIColorFromRGB(0x7665A3);
    [self.view addSubview:self.phoneNumLabel];
    [self.phoneNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneStatesCodeNumLabel.mas_top);
        make.height.equalTo(self.phoneStatesCodeNumLabel.mas_height);
        make.left.equalTo(self.phoneStatesCodeNumLabel.mas_right);
    }];
    
    self.verifyCodeTextFiled = [[UITextField alloc]init];
    self.verifyCodeTextFiled.placeholder = @"请输入验证码";
    self.verifyCodeTextFiled.tintColor = UIColorFromRGB(0x4A4A4A);
    self.verifyCodeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.verifyCodeTextFiled];
    [self.verifyCodeTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.equalTo(self.view.mas_left).offset(53);
        make.top.equalTo(self.phoneStatesCodeNumLabel.mas_bottom).offset(70);
    }];
    [self.verifyCodeTextFiled setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.verifyCodeTextFiled setValue:UIColorFromRGBA(0xA1A7AE, .6f) forKeyPath:@"_placeholderLabel.textColor"];
    
    self.resentVerifyCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.resentVerifyCodeBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
    self.resentVerifyCodeBtn.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [self.resentVerifyCodeBtn setTitleColor:UIColorFromRGB(0x4A4A4A) forState:UIControlStateNormal];
    [self.resentVerifyCodeBtn addTarget:self action:@selector(resentVerifyCodeBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.resentVerifyCodeBtn];
    [self.resentVerifyCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verifyCodeTextFiled.mas_top);
        make.height.equalTo(@20);
        make.right.equalTo(self.view.mas_right).offset(-53);
    }];
    
    UIView *bottomLineView = [[UIView alloc]init];
    bottomLineView.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verifyCodeTextFiled.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.verifyCodeTextFiled.mas_left);
        make.right.equalTo(self.resentVerifyCodeBtn.mas_right);
    }];
    
    self.timeLimitLabel = [[UILabel alloc]init];
    self.timeLimitLabel.text = @"验证码于30分钟内输入有效";
    self.timeLimitLabel.font = [UIFont systemFontOfSize:13];
    self.timeLimitLabel.textColor  = UIColorFromRGB(0xCDCDCD);
    self.timeLimitLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.timeLimitLabel];
    [self.timeLimitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomLineView).offset(20);
        make.height.equalTo(@20);
        make.width.equalTo(self.view.mas_width);
    }];
    
    
    self.submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.submitBtn.titleLabel.font = [UIFont systemFontOfSize:22.f];
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xFFB32C)] forState:UIControlStateHighlighted];
    
    [self.submitBtn addTarget:self action:@selector(submitBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.submitBtn];
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@56);
        make.left.equalTo(self.verifyCodeTextFiled.mas_left);
        make.right.equalTo(self.resentVerifyCodeBtn.mas_right);
        make.top.equalTo(self.timeLimitLabel.mas_bottom).offset(30);
    }];
    self.submitBtn.layer.cornerRadius = 28;
    self.submitBtn.layer.masksToBounds = YES;
    self.submitBtn.enabled = NO;
}

- (void)resentVerifyCodeBtnTouched{
    

}

- (void)submitBtnTouched{
    
    NSString *toast = [NSString stringWithFormat:@"验证码输入有误，请重新填写。"];
    [self.view makeToast:toast duration:2.0 position:CSToastPositionCenter];
    
    LVResetPasswordViewController *vc = [[LVResetPasswordViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [self submitBtnTouched];
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(NSNotification*)notification{
    
    if ([self.verifyCodeTextFiled.text length])
    {
        self.submitBtn.enabled = YES;
        [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xFFCD00)] forState:UIControlStateNormal];
        
    }else{
        self.submitBtn.enabled = NO;
        [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.verifyCodeTextFiled.text length])
    {
        self.submitBtn.enabled = YES;
        [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xFFCD00)] forState:UIControlStateNormal];
        
    }else{
        self.submitBtn.enabled = NO;
        [self.submitBtn setBackgroundImage:[self.submitBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.verifyCodeTextFiled resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
